package com.example.threadmodel;

import com.example.threadmodel.model.Action;
import com.example.threadmodel.service.ThreadManagerService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@SpringBootTest
public class Demo3_ManyPlayerTaskAvgInfo {
    @Autowired
    private ThreadManagerService threadManagerService;

    // 每个线程执行过的任务数量
    private final ConcurrentHashMap<String, AtomicInteger> threadName2TaskCountMap = new ConcurrentHashMap<>();
    /*
     模拟100个人，每个人100个任务，每个线程任务分配的情况
     打印线程均匀 {
     ACTION_THREAD--p2-t23=312,
      ACTION_THREAD--p2-t22=312,
      ACTION_THREAD--p2-t25=312,
      ACTION_THREAD--p2-t24=312,
      ACTION_THREAD--p2-t21=312,
      ACTION_THREAD--p2-t20=312,
      ACTION_THREAD--p2-t19=312,
      ACTION_THREAD--p2-t16=313,
      ACTION_THREAD--p2-t15=313,
      ACTION_THREAD--p2-t18=313,
      ACTION_THREAD--p2-t17=313,
      ACTION_THREAD--p2-t12=313,
      ACTION_THREAD--p2-t11=312,
      ACTION_THREAD--p2-t8=313,
      ACTION_THREAD--p2-t14=312,
      ACTION_THREAD--p2-t13=312,
      ACTION_THREAD--p2-t9=313,
      ACTION_THREAD--p2-t6=313,
      ACTION_THREAD--p2-t30=312,
      ACTION_THREAD--p2-t7=314,
      ACTION_THREAD--p2-t10=313,
      ACTION_THREAD--p2-t4=313,
      ACTION_THREAD--p2-t32=312,
      ACTION_THREAD--p2-t5=313,
      ACTION_THREAD--p2-t31=312,
      ACTION_THREAD--p2-t2=313,
      ACTION_THREAD--p2-t3=314,
      ACTION_THREAD--p2-t1=313,
      ACTION_THREAD--p2-t27=312,
      ACTION_THREAD--p2-t26=312,
      ACTION_THREAD--p2-t29=312,
      ACTION_THREAD--p2-t28=311
      }
     */
    @Test
    void test1() throws Exception {
        int playerNum = 100; // 在线玩家数
        int taskCount = 100; // 每个人的任务数

        CountDownLatch countDownLatch = new CountDownLatch(playerNum * taskCount);

        // 不断提交任务
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            for (int i = 0; i < playerNum; i++) {
                String queueId = i + "";

                for(int j = 0; j < taskCount; j++) {
                    threadManagerService.enqueueAction(new Action<String>(queueId, () -> {

                        // 执行时看看都在哪个线程
                        String threadName = Thread.currentThread().getName();

                        threadName2TaskCountMap.computeIfAbsent(threadName, k -> new AtomicInteger()).incrementAndGet();

                        // sleep一下
                        TimeUnit.MILLISECONDS.sleep(2);

                        countDownLatch.countDown();

                        return queueId;
                    }));
                }
            }

        }, 0, 1000, TimeUnit.MILLISECONDS);

        countDownLatch.await();

        log.warn("打印线程均匀 {}",  threadName2TaskCountMap);

        TimeUnit.SECONDS.sleep(Long.MAX_VALUE);
    }
}
